<?php
defined('_JEXEC') or die;

require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
if( $detect->isMobile() )
{
	include('default_mobile.php');
	return;
}

if(isset($this->data->groups[5]))://[V]user is teacher
?>
<style type="text/css">
	.btnUpL,.btnDnL{
		height:39px;width:40px;text-indent: -99999px;float:left;display:block;
	}
.btnUpL{
	background: url(/templates/fitplan/src/images/btnUpL.png) no-repeat;margin-left:5px;
}
.btnDnL{
	background: url(/templates/fitplan/src/images/btnDownL.png) no-repeat;margin:6px 0 0 5px;
}
.btnUpL:hover{cursor: pointer;}
</style>

<div class="wrapper" style="min-height:500px">
	<div class="content" id="createAccount-page">
		<div class="col-wrapper clearfix">
			<div class="list-user clearfix">
			
				<ul class="breadcrumb2 mb10">
					<li><a href="#">Mine Brugere</a></li>
				</ul>
				<div class="search">
				<form name="studentByName" action="index.php" method="post">
					<input name="name" placeholder="Skriv et navn…"<?php if($this->search) echo ' value="',$this->search,'"'?>>
					<a class="iconSearch" onclick="document.studentByName.submit()">Go</a>
					<input type="submit" style="position: absolute; left:-9999px"/>
					<input type="hidden" name="option" value="com_users" />
					<input type="hidden" name="view" value="profile" />
					<?php echo JHtml::_('form.token'); ?>
				</form>
				</div>
				
				<div class="clear"></div>
				
<form method="post" enctype="multipart/form-data">
		<input type="file" onchange="if(this.value) this.form.submit()" id="fileimg" style="display: none" name="stufile2" />
		<input type="hidden" id="stud" value="<?php echo $item->id?>" name="stud"/>
		<input type="hidden" value="com_users" name="option"/>
		<input type="hidden" value="user.upload_student_file" name="task"/>
</form>

				<ul class="user clearfix">
<?php
$i=0;
foreach($this->items as $item):
?>
					<li<?php if($i) echo ' class="mr0"'?>>
						<a href="#">
						   <?php
						      $dbo=JFactory::getDbo();
						      $query="select * from #__student_month WHERE id_user=".$item->id."";
						      $dbo->setQuery($query);
						      $src=$dbo->loadObject()->image;
						   ?>
						   <img src="images/student/<?php echo $src?>" alt="" width="110" height="141">
						</a>
						<p class="mb0">Profil-nr. <span><?php echo str_pad( $item->id,5 , 0, STR_PAD_LEFT)?></span></p>
						<p class="mb0">Navn: <span><?php echo $item->name?></span></p>
						<p class="mb3">E-mail: <span><a href="mailto:<?php echo $item->email,'">',$item->email?></a></span></p>
						<a class="btnSee-account" href="index.php?option=com_users&view=profile&layout=student&user_id=<?php echo $item->id?>">Se Konto</a>
						<a class="btnSkype" href="skype:<?php echo $item->skype?>?chat">Skype</a>
						<a class="btnUpL" onclick="jQuery('#stud').val(<?php echo $item->id?>);jQuery('#fileimg').click();" title="Store the plan (Excel file) to re-use in future">upload</a>
						<a class="btnSee-profile" href="index.php?option=com_users&view=profile&layout=student_checkplan&user_id=<?php echo $item->id?>">Se Profil</a>
						<a class="btnNote btn<?php echo $item->id?>" data-reveal-id="f-note" title="<?php echo $item->note?>" onclick="loadnote(<?php echo $item->id?>, '<?php echo $item->name?>',this.title)">Note</a>
						<?php $tmp=$item->id.".xls";if(file_exists("files/$tmp") || ($tmp=$item->id.".xlsx") && file_exists("files/$tmp")){?><a href="/files/<?php echo $tmp?>" class="btnDnL" title="Get the recent plan">download</a><?php }?>
					</li>
<?php
$i=1-$i;
endforeach;?>
				</ul>
				
			</div>
		</div>
	</div>
</div>

<div id="f-note" class="reveal-modal">
	<div class="f-note clearfix">
		<form id="nform">
			<h1 id="ntitle">&nbsp;</h1>
			<div class="login-event clearfix">
				<div class="event-content clearfix">
					<textarea name="note" placeholder="Dette eren"></textarea>
				</div><!--.event-content-->
				<a class="btnSave2" onclick="$('#close-reveal-modal').click()">Gem</a>
			</div>
			<input type="hidden" name="option" value="com_users" />
			<input type="hidden" name="uid" />
			<input type="hidden" name="task" value="user.takenote" />
			<?php echo JHtml::_('form.token'); ?>
		</form>
	</div>
	<a id="close-reveal-modal" class="close-reveal-modal"></a>
</div>

<script type="text/javascript">
	function loadnote(d, n, s){
		$("#ntitle").html(n+": note");
		$("#nform")[0].note.value=s;
		$("#nform")[0].uid.value=d;
		$(".btnSave2").click(function(){savenote(d)});
	}
	function savenote(d){
		dat=$("#nform").serialize();
		$.post('<?php echo JURI::root()?>index.php?', dat+'&no_html=1', function(){
				$(".btn"+d).attr("title",$("#nform")[0].note.value);//update the note
			});
	}
</script>
<?php
else://									Student layout

$m_start=intval(substr($this->data->registerDate,5,2));
$y_start=intval(substr($this->data->registerDate,0,4));
$m_now=intval(date("m"));
$y_now=intval(date("Y"));

if($y_start!=$y_now)
	$m_now+=12;

$map=array(0,'Januar','Februar','Marts','April','Maj','Juni',
'Juli','August','September','Oktober','November','December',
'Januar','Februar','Marts','April','Maj','Juni',
'Juli','August','September','Oktober','November','December');
$period= 12;//$this->data->information['length']==1 ? 12 : $this->data->information['length'];
$tpath=JURI::base().'templates/'.JFactory::getApplication()->getTemplate();

	function mySql2PhpTime($sqlDate){
		$arr = date_parse($sqlDate);
		return mktime($arr["hour"],$arr["minute"],$arr["second"],$arr["month"],$arr["day"],$arr["year"]);
	}

$db=JFactory::getDbo();
$db->setQuery("SELECT * FROM #__student_month WHERE id_user=".$this->data->id." ORDER BY id");
$rows=$db->loadObjectList();

//count items
$db->setQuery("SELECT registerDate FROM `#__users` WHERE id=".$this->data->id);
$regDate=$db->loadResult();$regDate=mySql2PhpTime($regDate);
$regYear=(int)date("y", $regDate);
$regDate=(int)date("W", $regDate);//regDate become regWeek

$db->setQuery("SELECT id,StartTime,EndTime,Color,RecurringRule FROM #__jqcalendar WHERE id_student=".$this->data->id." AND type=0");
$items=$db->loadObjectList();
$yes=0;$no=0;
foreach($items as $item){

	if($item->Color==10)
		$yes++;
	else if($item->Color==2)
		$no++;
//count real items

	if($item->RecurringRule){

		$item->RecurringRule=json_decode($item->RecurringRule);

		$sDate=mySql2PhpTime($item->StartTime);
		$sYear=(int)date("y", $sDate);
		$sWeek=(int)date("W", $sDate);

		$weeks=$item->RecurringRule->weeks;
		$weekdays=$item->RecurringRule->weekdays;

		if($regYear != $sYear)//is the same year?
			$sWeek=52+$sWeek;

		if(isset($item->RecurringRule->data)){

			$tmp=0;$tmp1=0;
			foreach($item->RecurringRule->data as $dat)
				foreach($dat as $dat0)
					if($dat0==10)
						$tmp1++;
					else if($dat0==2)
						$tmp++;

			$yes+=$tmp1;$no+=$tmp;
		}
	}
}///count
?>
	<link href="<?php echo$tpath?>/calendar/css/dailog.css" rel="stylesheet" type="text/css" />
	<link href="<?php echo$tpath?>/calendar/css/calendar.css" rel="stylesheet" type="text/css" />
	<link href="<?php echo$tpath?>/calendar/css/dp.css" rel="stylesheet" type="text/css" />
	<link href="<?php echo$tpath?>/calendar/css/alert.css" rel="stylesheet" type="text/css" />
	<link href="<?php echo$tpath?>/calendar/css/main.css" rel="stylesheet" type="text/css" />

<div class="wrapper">
	<div class="content clearfix" id="myprofile-page">
		<h1 class="hd">Personlig profil</h1>
		<div class="info-pf">
			<div class="w320 fl">
				<span>Profil-nr. <?php echo $this->data->id;?></span>
				<span>Navn: <?php echo $this->data->name;?></span>
				<span>Startvægt: <?php echo $this->data->Vægt;?> kg.</span>
			</div>
			<div class="w320 fl">
<?php
$db->setQuery("SELECT StartTime FROM #__jqcalendar WHERE id_student=".$this->data->id." AND type=1");
$tmp=$db->loadResultArray();
$now=time();

foreach($tmp as $v){
	$v=mySql2PhpTime($v);
	if($v && $v >= time()){
		$d=date("d.m.Y", $v);
		$tmp=$d. " Kl. " .date("H.i", $v);
		break;
	}
}
if(is_array($tmp))
	$tmp="";
?>
				<span>Personlig vejleder: <?php echo $this->data->teacher_name?></span>
				<span>Næste webmøde: <?php echo $tmp?></span>
				<!--span>Køb mere tid hos din vejleder <a href="#">her</a></span-->
				<span><a href="<?php echo JRoute::_("index.php?option=com_users&view=profile&layout=edit&Itemid=117")?>" style="color:blue">Rediger brugeroplysninger</a></span>
			</div>
		</div>
		<div class="clear"></div>

		<div id="page-wrap">
			<div id="tabs">
				<ul>
<?php //Month tabs
	for($i=$m_start,$j=0;$j<$period;$j++,$i++){?>
					<li class="ui-state-default ui-corner-top uitab<?php echo $i;if($i==$m_now) echo ' ui-tabs-selected'?>"><a <?php if($i<=$m_now) echo 'onclick="formdisplay('.$i.')"'?>><?php echo $map[$i]?></a></li>
<?php
	}?>
				</ul>

<?php
	for($i=$m_start,$j=0;$i<=$m_now;$j++,$i++){?>
				<div id="fragment-<?php echo $i?>" class="ui-tabs-panel" style="<?php if($i!=$m_now) echo 'display:none'?>">
<form class="register-form" method="post" action="" id="save_month<?php echo $i;?>" enctype="multipart/form-data">
<?php
$tmp=explode(";", $rows[$j]->event);
if(isset($w[0])) $lastw= $w[0];
?>
					<div class="result clearfix">
						<div class="fl w185">
<?php if($m_now==$i){
	//Upload image available on current month
	?>
							<h4><a onclick="jQuery('#fileimg').click()">Opload billede</a></h4>
							<input type="file" id="fileimg" style="display:none" name="image_student" />
<?php }?>
							<div class="avata-sub">
<?php //auto fit box
if($rows[$j]->image)
	$tmp1='images/student/'.$rows[$j]->image;
$h='';
if(file_exists($tmp1))
{
	list( $w, $h)=getimagesize($tmp1);
	$h=$h > $w ? 'height="240"' : $h='width="185"';
}
?>
								<img <?php if($m_now==$i){?>id="preview"<?php }?> <?php echo $h?> <?php echo 'src="'.JURI::root(),$tmp1,'"'?> alt="portrait (size 185x240)">
							</div>
						</div>

						<div class="fr w703">
							<div class="fl" style="width:264px">
<?php //statistic data
$w=array_map(function($item){$item=explode(":",$item);return $item[1];}, $tmp);?>
		<p>Indtast denne måned vægt:<input class="input_vu" value=" kg" disabled/><input class="input_nam" value="<?php echo $w[0] ? : ($i==$m_start ? $this->data->Vægt : "")?>" name="vagt"/></p>
		<p>Indtast mål på talje:<input class="input_vu" value=" cm" disabled/><input class="input_nam" value="<?php echo $w[1]?>" name="talje"/></p>
		<p>Indtast mål på hofte:<input class="input_vu" value=" cm" disabled/><input class="input_nam" value="<?php echo $w[2]?>" name="hofte"/></p>
		<p>Indtast mål på bryst:<input class="input_vu" value=" cm" disabled/><input class="input_nam" value="<?php echo $w[3]?>" name="bryst"/></p>
		<p>Indtast mål på overarm:<input class="input_vu" value=" cm" disabled/><input class="input_nam" value="<?php echo $w[4]?>" name="bryst1"/></p>
				<p class="mt40 mb5">Resultat af tjekplan:</p>
				<p class="green mb5">JA (<?php echo $yes?>)</p>
				<p class="red mb5">NEJ (<?php echo $no?>)</p>
							</div>
							<div class="fl ml80" style="width:345px">
								<p>Kg. tilbage til slutmålet: <span><?php echo $w[0] ? $this->data->information['weightdown'] - $this->data->Vægt + $w[0] : ($i==$m_start ? $this->data->information['weightdown'] : '-')//how much weight remain?> kg.</span></p>
								<p>Tabt siden start: <span><?php echo !empty($w[0]) ? $this->data->Vægt-$w[0] : '-'//offset compare to goal?> kg.</span></p>
								<p>Tabt siden sidste måned: <span><?php echo $lastw && !empty($w[0]) ? $lastw-$w[0] : '-'//offset compare to last month?> kg.</span></p>
								<!--p>Find din indkøbsliste <a href="#">her</a></p-->
								<label style="font-size: 1.2em;font-weight: 700;">Indtast notater her</label>
								<textarea name="note"><?php
								echo !empty($rows[$j]->note) ? $rows[$j]->note : "Uge 1:

Uge 2:

Uge 3:

Uge 4:";
								?></textarea>
							</div>
						</div>
<div class="clear"></div>
<?php if($rows[0]->file){
	//PDF files
	$tmp2=explode(':',$rows[0]->file);
?>
						<div style="margin-left: 29px;width:106px">
<?php
	if(strpos($rows[0]->file,"kost")!==FALSE){
		$tmp2[0];
?>
					<a href="/files/<?php echo $tmp2[0]?>" style="display:block;height:24px" target="_blank"><img src="<?php echo $tpath?>/src/images/btn_Dietplan.png"/></a>
<?php	}
	if(strpos($rows[0]->file,"indkob")!==FALSE){
		$tmp2= isset($tmp2[1]) ? $tmp2[1] : $tmp2[0];
?>
					<a href="/files/<?php echo $tmp2?>" style="display:block;height:24px;margin-top:5px" target="_blank"><img src="<?php echo $tpath?>/src/images/btn_Shopping.png"/></a>
<?php	}?>
						</div>
<?php }?>

						<div class="status">
							<a class="fl bntSave" onclick="jQuery('#save_month<?php echo $i?>').submit();">Gem</a>
<?php if($i>$m_start){?>
							<a class="bntPrevious2 fl ml425" rel="<?php echo $i-1?>" onclick="formdisplay(this.rel)">Forrige</a>
<?php }?>
<?php if($i<$m_now){?>
							<a class="bntNext2 fr" rel="<?php echo $i+1?>" onclick="formdisplay(this.rel)">Næste</a>
<?php }?>
						</div>
					</div>

	<input type="hidden" value="<?php echo $i?>" name="month"/>
	<input type="hidden" value="com_users" name="option"/>
	<input type="hidden" value="user.save_month_student" name="task"/>
	<input type="hidden" value="<?php echo $this->data->id?>" name="id_user"/>
</form>
				</div>
<?php
	}//endfor
	?>
			</div>
		</div>
		<div class="calendar mt20">
	<div>
	
		<div id="calhead" style="padding-left:1px;padding-right:1px;">
			<div class="cHead"><div class="ftitle">Resultater fra hjælpe app</div>
			<div id="loadingpannel" class="ptogtitle loadicon" style="display: none;">Loading data...</div>
			 <div id="errorpannel" class="ptogtitle loaderror" style="display: none;">Sorry, could not load your data, please try again later</div>
			</div>

			<div id="caltoolbar" class="ctoolbar">
			<div id="showdaybtn" class="fbutton">
				<div><span title='Day' class="showdayview">Day</span></div>
			</div>
				<div	id="showweekbtn" class="fbutton fcurrent">
				<div><span title='Week' class="showweekview">Week</span></div>
			</div>
				<div id="showmonthbtn" class="fbutton">
				<div><span title='Month' class="showmonthview">Month</span></div>

			</div>
			<div class="btnseparator"></div>
				<div id="showreflashbtn" class="fbutton">
				<div><span title='Refresh view' class="showdayflash">Refresh</span></div>
				</div>
			 <div class="btnseparator"></div>
			<div id="sfprevbtn" title="Prev" class="fbutton">
				<span class="fprev"></span>

			</div>
			<div id="sfnextbtn" title="Next" class="fbutton">
				<span class="fnext"></span>
			</div>
			<div class="fshowdatep fbutton">
					<div>
						<input type="hidden" name="txtshow" id="hdtxtshow" />
						<span id="txtdatetimeshow">Loading</span>

					</div>
			</div>

			<div class="clear"></div>
			</div>
		</div>
		<div style="padding:1px;">

		<div class="t1 chromeColor">
			&nbsp;</div>
		<div class="t2 chromeColor">
			&nbsp;</div>
		<div id="dvCalMain" class="calmain printborder">
			<div id="gridcontainer" style="overflow-y: visible;">
			</div>
		</div>
		<div class="t2 chromeColor">

			&nbsp;</div>
		<div class="t1 chromeColor">
			&nbsp;
		</div>
		</div>

	</div>
	
	<script src="<?php echo$tpath?>/calendar/src/Plugins/common.js" type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/datepicker_lang_US.js" type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/jquery.datepicker.js" type="text/javascript"></script>

	<script src="<?php echo$tpath?>/calendar/src/Plugins/jquery.alert.js" type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/jquery.ifrmdailog.js" defer type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/wdCalendar_lang_US.js" type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/jquery.calendar.js" type="text/javascript"></script>
<script type="text/javascript">
	$(document).ready(function(){
		var view="week";

		var DATA_FEED_URL = "<?php echo JURI::base()?>index.php?option=com_datafeed&no_html=1";
		var op = {
			view: view,
			theme:0,
			showday: new Date(),
			ViewCmdhandler:View,
			onWeekOrMonthToDay:wtd,
			onBeforeRequestData: cal_beforerequest,
			onAfterRequestData: cal_afterrequest,
			onRequestDataError: cal_onerror,
			autoload:true,
			readonly: true,
			url: DATA_FEED_URL + "&method=list&id=<?php echo $this->data->id?>",
		};
		var $dv = $("#calhead");
		var _MH = document.documentElement.clientHeight;
		var dvH = $dv.height() + 2;
		op.height = _MH - dvH;
		op.eventItems =[];

		var p = $("#gridcontainer").bcalendar(op).BcalGetOp();
		if (p && p.datestrshow) {
			$("#txtdatetimeshow").text(p.datestrshow);
		}
		$("#caltoolbar").noSelect();

		$("#hdtxtshow").datepicker({ picker: "#txtdatetimeshow", showtarget: $("#txtdatetimeshow"),
		onReturn:function(r){
				var p = $("#gridcontainer").gotoDate(r).BcalGetOp();
				if (p && p.datestrshow) {
					$("#txtdatetimeshow").text(p.datestrshow);
				}
		}
		});
		function cal_beforerequest(type)
		{
			var t="Loading data...";
			switch(type)
			{
				case 1:
				t="Loading data...";
				break;
				case 2:
				case 3:
				case 4:
				t="The request is being processed ...";
				break;
			}
			$("#errorpannel").hide();
			$("#loadingpannel").html(t).show();
		}
		function cal_afterrequest(type)
		{
			switch(type)
			{
				case 1:
					$("#loadingpannel").hide();
					break;
				case 2:
				case 3:
				case 4:
					$("#loadingpannel").html("Success!");
					window.setTimeout(function(){ $("#loadingpannel").hide();},2000);
				break;
			}

		}
		function cal_onerror(type,data)
		{
			$("#errorpannel").show();
		}
		function View(data)
		{
			var d=data[2].getDate()+"";
			var M=(data[2].getMonth()+1)+"";
			var y=data[2].getFullYear()+"";
			var m=data[2].getMinutes()+"";
			var h=data[2].getHours()+"";
			data[2]=strpad(d)+"-"+strpad(M)+"-"+y+" "+strpad(h)+":"+strpad(m);
			d=data[3].getDate()+"";
			M=(data[3].getMonth()+1)+"";
			y=data[3].getFullYear()+"";
			m=data[3].getMinutes()+"";
			h=data[3].getHours()+"";
			data[3]=strpad(d)+"-"+strpad(M)+"-"+y+" "+strpad(h)+":"+strpad(m);
			var str = "Dato: "+data[2] + " - " + data[3]+"\n\nEmne: "+data[1]+(data[9] ? "\nSted: "+data[9]:"");
			alert(str);
		}
		function strpad(str, length, pad){
			if(!length) length=2;
			if(!pad) pad="0";
			return Array(length+1 - str.length).join(pad) + str;
		}
		function wtd(p)
		{
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}
			$("#caltoolbar div.fcurrent").each(function() {
				$(this).removeClass("fcurrent");
			})
			$("#showdaybtn").addClass("fcurrent");
		}
		//to show day view
		$("#showdaybtn").click(function(e) {
			//document.location.href="#day";
			$("#caltoolbar div.fcurrent").each(function() {
				$(this).removeClass("fcurrent");
			})
			$(this).addClass("fcurrent");
			var p = $("#gridcontainer").swtichView("day").BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}
		});
		//to show week view
		$("#showweekbtn").click(function(e) {
			//document.location.href="#week";
			$("#caltoolbar div.fcurrent").each(function() {
				$(this).removeClass("fcurrent");
			})
			$(this).addClass("fcurrent");
			var p = $("#gridcontainer").swtichView("week").BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}

		});
		//to show month view
		$("#showmonthbtn").click(function(e) {
			//document.location.href="#month";
			$("#caltoolbar div.fcurrent").each(function() {
				$(this).removeClass("fcurrent");
			})
			$(this).addClass("fcurrent");
			var p = $("#gridcontainer").swtichView("month").BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}
		});

		$("#showreflashbtn").click(function(e){
			$("#gridcontainer").reload();
		});
		//previous date range
		$("#sfprevbtn").click(function(e) {
			var p = $("#gridcontainer").previousRange().BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}

		});
		//next date range
		$("#sfnextbtn").click(function(e) {
			var p = $("#gridcontainer").nextRange().BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}
		});

	});
</script>
		</div>
	</div>
</div>

<script type="text/javascript">
var cur=<?php echo $m_now?>;
var w_1=0;
	function formdisplay(x){
		pre1='fragment-';
		pre2='uitab';
		if(cur!=x){
			jQuery("#"+pre1+cur).hide();
			jQuery("."+pre2+cur).removeClass('ui-tabs-selected');
		}
		jQuery("#"+pre1+x).show();
		jQuery("."+pre2+x).addClass('ui-tabs-selected');
		cur=x;
	}

function previewImg(inimg, outimg){
	if(!outimg)
		outimg='preview';
	tmp=document.getElementById(outimg);

	if (inimg.files && inimg.files[0]){
		if(window.FileReader){
			var fr = new FileReader();
			fr.onload = function(e){tmp.src=e.target.result};

			fr.readAsDataURL(inimg.files[0]);
		}
		return;
	}
	tmp.src = inimg.value;
}
jQuery("#fileimg").change(function(){previewImg(this)});
</script>
<?php endif;//[V] end student layout?>